<?php
	class PersonelDatabase{
		public function controlLogin($mypersonelid, $mypassword){
		?>
			
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" type="text/css" href="style.css" />
		<title>Wellcome to Charity Organization!</title>
		</head>
		
		<body>
		<div id="container">
				<div id="header">
					<h1>Charity<span class="off">Organization</span></h1>
				</div>   
				<div id="menu">
				
				</div>
			
				<div id="content">
				<div id="content_top"></div>
				<div id="content_main">
				<?php
				$link =mysql_connect('localhost','root','');
				if (!$link) {
					die('Not connected : ' . mysql_error());
				}
				$db_selected = mysql_select_db('seproject', $link);
				if (!$db_selected) {
					die ('Cannot useSeproject : ' . mysql_error());
				}
				$query=mysql_query("SELECT PersonelID,Password FROM `seproject`.`personel`");
				$help=0;
				for($i=0;$i<mysql_num_rows($query);$i++)
				{
					$a=mysql_fetch_row($query);
					if ($mypersonelid==$a[0] && $mypassword==$a[1])
					{
						echo "Welcome ".$mypersonelid."!";
						$_SESSION["PersonelID"]=$mypersonelid;
						$help=1;	
						?>
						<a href="homeP.php">Go to homepage</a>
						<?php	
					}
				}
				if($help==0)
				{
					echo "PersonelID or password is wrong!"?>
					<a href="index.php">Back</a>
					<?php
				}
				?>
				</div>
				<div id="content_bottom"></div>
			</div>
		</div>
		</body>
		</html>
		<?php
		
		}
		
		public function sendPass($mypersonelid, $mycaptcha, $mycode){
			
			?>
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<link rel="stylesheet" type="text/css" href="style.css" />
			<title>Wellcome to Charity Organization!</title>
			</head>
	
			<body>
			<div id="container">
				<div id="header">
					<h1>Charity<span class="off">Organization</span></h1>
				</div>   
				<div id="menu">
				</div>
		
				<div id="content">
				<div id="content_top"></div>
				<div id="content_main">
		<?php
			$link =mysql_connect('localhost','root','');
		if (!$link) {
			die('Not connected : ' . mysql_error());
		}
		$db_selected = mysql_select_db('seproject', $link);
		if (!$db_selected) {
			die ('Cannot useSeproject : ' . mysql_error());
		}
		if(isset($mycaptcha)&&$mycaptcha!=""&&$mycode==$mycaptcha)
		{
			//echo "Correct Code Entered";
			$PersonelID=$mypersonelid;
			$query=mysql_query("SELECT Email,Password FROM personel where `PersonelID`='$PersonelID'");
			$a=mysql_fetch_row($query);
			$Email=$a[0];
			$Password=$a[1];
			
			$to = $Email;
			$subject = "Forgot Password";
			$message = "Hello! Dear"+$PersonelID+"!/nThis is your password:	"+$Password;
			$from = "someonelse@example.com";
			$headers = "From:" . $from;
			mail($to,$subject,$message,$headers);
			echo "Your password has been sent to your mail.";
			?>
			<a href="index.php">Go to homepage</a>	
			<?php
		}
		else
		{
			echo "Wrong Code Entered!";
			?>
			<a href="ForgetPassP.php">Back</a>	
			<?php
		}
		
		}
		public function controlLogout(){
		
		?>
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<link rel="stylesheet" type="text/css" href="style.css" />
			<title>Wellcome to Charity Organization!</title>
			</head>
	
			<body>
			<div id="container">
				<div id="header">
					<h1>Charity<span class="off">Organization</span></h1>
				</div>   
				<div id="menu">
				</div>
		
				<div id="content">
				<div id="content_top"></div>
				<div id="content_main">
		<?php
			$link =mysql_connect('localhost','root','');
			if (!$link) {
				die('Not connected : ' . mysql_error());
			}
			$db_selected = mysql_select_db('seproject', $link);
			if (!$db_selected) {
				die ('Cannot useSeproject : ' . mysql_error());
			}
			session_destroy();
					
			echo "You have successfully logged out.";
			?>
			<a href="index.php">Go to homepage</a>	
			<?php
		}
		
		public function controlChangeProfile($myfirstname, $mylastname,$myemail,$mycellphone, $myaddress)
		{
		?>
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" type="text/css" href="style.css" />
		<title>Wellcome to Charity Organization!</title>
		</head>
		
		<body>
		<div id="container">
				<div id="header">
					<h1>Charity<span class="off">Organization</span></h1>
				</div>   
				<div id="menu">
				</div>
			
				<div id="content">
				<div id="content_top"></div>
				<div id="content_main">
				<?php
				$link =mysql_connect('localhost','root','');
				if (!$link) {
					die('Not connected : ' . mysql_error());
				}
				$db_selected = mysql_select_db('seproject', $link);
				if (!$db_selected) {
					die ('Cannot useSeproject : ' . mysql_error());
				}
				$PersonelID=$_SESSION["PersonelID"];
				$FirstName=$myfirstname;$LastName=$mylastname;$Email=$myemail;$CellPhoneNumber=$mycellphone;$Address=$myaddress;
				$sql = mysql_query("update `seproject`.`personel` set `FirstName`='$FirstName' ,`LastName`='$LastName',`Email`='$Email' , `CellPhoneNumber`='$CellPhoneNumber',`Address`='$Address' where `PersonelID`='$PersonelID'");
				if($sql)
				{
					echo "Your profile information has been changed!";
					?>
					<a href="homeP.php">Go to homepage</a>
					<?php
				}
				else
				{
					echo "Change information has been failed!";
					?>
					<a href="EditProfileP.php">Back</a>
					<?php
				}
				?>
				</div>
				<div id="content_bottom"></div>
			</div>
		</div>
		</body>
		</html>
		<?php
		}
		public function controlChangePassword($mycurpass, $mynewpass, $myrepass)
		{
			?>
			<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" type="text/css" href="style.css" />
		<title>Wellcome to Charity Organization!</title>
		</head>

		<body>
		<div id="container">
		<div id="header">
			<h1>Charity<span class="off">Organization</span></h1>
        </div>   
        <div id="menu">
        </div>
    
		<div id="content">
        <div id="content_top"></div>
		<div id="content_main">
		<?php
		$link =mysql_connect('localhost','root','');
		if (!$link) {
			die('Not connected : ' . mysql_error());
		}
		$db_selected = mysql_select_db('seproject', $link);
		if (!$db_selected) {
			die ('Cannot useSeproject : ' . mysql_error());
		}
		$PersonelID=$_SESSION["PersonelID"];
		$CurPassword=$mycurpass;
		$NewPassword=$mynewpass;
		$retypeNewPassword=$myrepass;
		
		
		$query=mysql_query("SELECT Password FROM personel where `PersonelID`='$PersonelID'");
		$a=mysql_fetch_row($query);
		$Password=$a[0];
		if($Password!=$CurPassword)
		{
			echo "Your current password is wrong! Retype Again!";
			?>
			<a href="EditPassP.php">Back</a>	
			<?php
		}
		else if($NewPassword!=$retypeNewPassword)
		{
			echo "Password doesn't match! Retype Again!";
			?>
			<a href="EditPassP.php">Back</a>	
			<?php
		}
		else
		{		
			$Password=$NewPassword;
			$sql = mysql_query("update personel set `Password`='$Password' where `PersonelID`='$PersonelID'");
			if($sql)
			{
				echo "Your password has been changed!";
				?>
				<a href="homeP.php">Go to homepage</a>
				<?php
			}
			else
			{
				echo "Change password has been failed!";
				?>
				<a href="EditPassP.php">Back</a>
				<?php
			}
		}
		?>
		</div>
        <div id="content_bottom"></div>
		</div>
		</div>
		</body>
		</html>
		<?php
		}
		
	}
?>